package com.ukao.steward.ui.valetRunners.gathering;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.adapter.PayTagAdapter;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.BalanceBean;
import com.ukao.steward.bean.OrderInfoBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.eventbus.PayEvent;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.T;
import com.ukao.steward.widget.StateButton;
import com.ukao.steward.widget.TagFlowLayout;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Observable;

import static com.ukao.steward.R.id.commit_btn;
import static com.ukao.steward.eventbus.PayEvent.Message.PAY_SUCCEED;


public class UnionpayFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.pay_amount)
    TextView payAmount;
    Unbinder unbinder;
    @BindView(R.id.remark)
    EditText remark;

    @BindView(R.id.layout)
    LinearLayout layout;
    @BindView(R.id.id_tagflowlayout)
    TagFlowLayout idTagflowlayout;
    @BindView(R.id.commit_btn)
    StateButton commitBtn;


    private String mParam1;
    private String mParam2;

    private OrderInfoBean mOrderInfoBean;
    private Observable observable;
    private PayTagAdapter mPayTagAdapter;


    public static UnionpayFragment newInstance(OrderInfoBean mOrderInfoBean, String param2) {
        UnionpayFragment fragment = new UnionpayFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, mOrderInfoBean);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mOrderInfoBean = getArguments().getParcelable(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_unionpay, container, false);
        unbinder = ButterKnife.bind(this, view);

        initView();
        return view;
    }

    @Override
    protected void initView() {
        payAmount.setText(CheckUtils.isEmptyNumber(mOrderInfoBean.getShouldPrice()));
        if (mOrderInfoBean.isEnableOther() && !CheckUtils.isEmpty(mOrderInfoBean.getPaySignList())) {
            mPayTagAdapter = new PayTagAdapter(_mActivity, mOrderInfoBean.getPaySignList());
            idTagflowlayout.setAdapter(mPayTagAdapter);

        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (mOrderInfoBean.isbundle()) {
            unlockOrder(mOrderInfoBean.getOrderId());
        }


    }


    /*支付解锁*/
    public void unlockOrder(String orderId) {
        commitBtn.setText("订单生成中...");
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores().unlockOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {
                            commitBtn.setText("确定收款");

                        } else {

                            T.show(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }

                });
    }


    public void unionPay() {

        showProgressDialog();

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());


      String  paySign="";
        if (mPayTagAdapter!=null&&mPayTagAdapter.getCount()!=0){
            int selectpos=-1;
            for (Integer pos : idTagflowlayout.getSelectedList()) {
                selectpos=pos;
            }
            if (selectpos!=-1){
                OrderInfoBean.PaySignListBean mitem = mPayTagAdapter.getItem(selectpos);
                paySign=mitem.getVal();
            }
        }


        if (mOrderInfoBean.isUseUPay()) {
            mHashMap.put("groupCardType", "41");//groupCardType 支付(团)卡类型 41-优付卡
        } else {
            mHashMap.put("balance", String.valueOf(mOrderInfoBean.getUseBalance()));
        }

        if (!CheckUtils.isEmpty(mOrderInfoBean.getCpnRelId())) {
            mHashMap.put("cpnRelId", mOrderInfoBean.getCpnRelId());
        }
        mHashMap.put("orderId", mOrderInfoBean.getOrderId());
        mHashMap.put("remark", getText(remark));
        if (!CheckUtils.isEmpty(paySign)){  /*银行卡选项*/
            observable = apiStores().otherPay(Constant.createParameter(mHashMap));
        }else{
            observable = apiStores().unionPay(Constant.createParameter(mHashMap));
        }

        addSubscription(observable,
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                            dismissProgressDialog();
                            mOrderInfoBean.setPayFinish(true);
                            PayEvent.postNoData(PAY_SUCCEED);
                        } else {
                            T.show(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }

                });
    }

    @OnClick({commit_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case commit_btn:
                if (commitBtn.getText().equals("确定收款")) {
                    unionPay();
                }

                break;
        }
    }
}
